Developer --> Technical Publications
PATHJava Developer Documentation > Mac OS Runtime for Java > JManager > Programming With JManager


Menu Selections

Both mouse events and keyboard events can select menu items. In either case, the event should be handled by a menu selection function. If the selection corresponds to a Java applet's menu item, you must pass the selection to the applet's AWT context using the JMMenuSelected function JMMenuSelected. Listing 1-18 shows a simple menu selection function.

Listing 1-18 Handling a menu item selection

/* enumerators to define the Mac OS menus*/
enum Menus {
    eAppleMenu = 1000,
    eFileMenu,
    eEditMenu,
    eLastMenu
    };

/* enumerators to define the menu items */
enum AppleMenuItems {
    eAboutItem = 1
    };

enum FileMenuItems {
    eMyAction1 = 1,     /* nonstandard menu item defined by */
                        /* the application */
    eQuitItem = eMyAction1 + 2
    };

enum EditMenuItems {
    eUndoItem = 1,
    eCutItem = eUndoItem + 2,
    eCopyItem,
    ePasteItem,
    eClearItem,
    eSelectAllItem = eClearItem + 2
    };

static void menuHit(short menuID, short menuItem)
{
    switch (menuID) {
        case eAppleMenu:
            switch (menuItem) {
                case eAboutItem:    /* show About box */
                    MyAboutBox();
                    break;
                    
                default: {      /* open the appropriate desk accessory */
                    Str255 s;
                    SetPort(LMGetWMgrPort());
                    GetItem(GetMHandle(eAppleMenu), menuItem, s);
                    if (s[0] > 0)
                        OpenDeskAcc(s);
                }   break;
            }
            break;
            
        case eFileMenu:
            switch (menuItem) {
                case eMyAction1:
                    DoMyAction1();
                    break;
                    
                case eQuitItem:
                    MainEventLoopContinues = false;
                    break;
            }
            break;
            
        case eEditMenu:
            break;
                
        default: {
            /* pass the menu hit to the AWTContext for processing */
            WindowPtr win = FrontWindow();
            if (win != nil) {
                JMFrameRef frame = (JMFrameRef) GetWRefCon(win);
                if (frame != nil) {
                    JMAWTContextRef context = JMGetFrameContext(frame);
                    if (context != nil)
                        JMMenuSelected(context, GetMHandle(menuID),
                            menuItem);
                }
            }
        }   break;
    }
    
    HiliteMenu(0);
}

If the user did not select a standard menu item, the menuHit function passes the menu selection to the applet's AWT context. On the Mac OS, the menu bar is always associated with the active window (and, consequently, with the active frame). After determining the frame associated with the window (using the Mac OS Toolbox function GetWRefCon ), JMGetFrameContext returns the AWT context associated with the frame. The menu handle of the selection is then passed to the AWT context using the JMMenuSelected functio n JMMenuSelected. Note that if you want to send additional modifier information to the AWT context, you should call the JMMenuSelectedWithModifiers function JMMenuSelectedWithModifiers instead.


© 1998 Apple Computer, Inc. — (Last Updated 3 Dec 98)

Previous | Back Up One Level | Next |